package com.ffapp.rainmonitorcustom.gis;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ffapp.rainmonitorcustom.R;
import com.ffapp.rainmonitorcustom.basis.BasisApp;
import com.ffapp.rainmonitorcustom.basis.BasisFragment;
import com.ffapp.rainmonitorcustom.basis.Constants;
import com.ffapp.rainmonitorcustom.bean.DeviceListBean;
import com.ffapp.rainmonitorcustom.bean.DeviceListData;
import com.ffapp.rainmonitorcustom.event.MessageEvent;
import com.ffapp.rainmonitorcustom.util.MyUtils;
import com.ffapp.rainmonitorcustom.video.VideoInfoActivity;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.loopj.android.http.RequestParams;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.ActivityTool;
import my.LogUtil;
import my.http.HttpRestClient;
import my.http.MyHttpListener;

/**
 * gis列表
 * */
public class GisListFragment extends BasisFragment {

    public static GisListFragment newInstance() {
        GisListFragment fragment = new GisListFragment();
        return fragment;
    }

    @BindView(R.id.recyclerview)
    XRecyclerView mRecyclerView;

    private MyAdapterList mAdapter;
    private DeviceListBean mListBean;

    private String keyword="";


    @Override
    public void initViews() {
        super.initViews();
        setContentView(R.layout.base_xrecycler_with_empty);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext,2);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setVerticalScrollBarEnabled(true);
        mRecyclerView.setLayoutManager(layoutManager);
//        mRecyclerView.setEmptyView(findViewById(R.id.refresh_view));
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mListBean.refresh();
                getList();
            }

            @Override
            public void onLoadMore() {
                getList();
            }
        });
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(false);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

        mListBean = new DeviceListBean();
        setListView();
//        mRecyclerView.refresh();
        getList();
//        getFromCache();
    }


    //    private void getFromCache() {
//
//        DothingsListBean beanCaceh = (DothingsListBean) DothingsListBean.getObjFromCache(id);
//        if (beanCaceh != null) {
//            mListBean = beanCaceh;
//            setListView();
//        } else {
//            mListBean = new ServiceNewListBean();
//            setListView();
//            getList();
//        }
//    }

    private void setListView() {
        if (mAdapter == null) {
            mAdapter = new MyAdapterList();
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }

    }

    private void onListViewComplete() {
        mRecyclerView.refreshComplete();
        mRecyclerView.loadMoreComplete();
        mRecyclerView.setLoadingMoreEnabled(mListBean.hasNextPage());
//        mRecyclerView.setLoadingMoreEnabled(false);
        setEmptyView();
    }

    private void setEmptyView() {
        if (mListBean == null || mListBean.getList().size() == 0) {
            findViewById(R.id.refresh_view).setVisibility(View.VISIBLE);
            ((ImageView)findViewById(R.id.refresh_img)).setImageResource(R.drawable.chart_img_empty);
            ((TextView)findViewById(R.id.refresh_text_empty)).setVisibility(View.VISIBLE);
            ((TextView)findViewById(R.id.refresh_text_empty)).setText("暂没有任何数据");
            findViewById(R.id.refresh_view).setOnClickListener(
                    new View.OnClickListener() {
                        public void onClick(View v) {
                            mRecyclerView.refresh();
                        }
                    });
        } else {
            findViewById(R.id.refresh_view).setVisibility(View.INVISIBLE);
        }
    }

    private void getList() {
        RequestParams params = new RequestParams();
//        showProgress();
//        params.put(BaseListDataListBean.PAGE_NAME, mListBean.getNextPage());
//        params.put(BaseListDataListBean.PAGE_SIZE_NAME, BaseListDataListBean.PAGE_SIZE);
        params.put("keyword",keyword);
        HttpRestClient.get(Constants.URL_IMG_LIST, params, new MyHttpListener() {
                    @Override
                    public void onSuccess(int httpWhat, Object result) {
                        DeviceListBean listBean = (DeviceListBean) result;
                        mListBean= listBean;
                        LogUtil.i("onSuccess---->"+listBean.getList().size());
                        setListView();
                    }

                    @Override
                    public void onFailure(int httpWhat, Object result) {
                        super.onFailure(httpWhat, result);
//            mListBean = new ServiceNewListBean();
                        setListView();
                    }

                    @Override
                    public void onFinish(int httpWhat) {
                        hideLoading();
                        onListViewComplete();
                    }
                },
                0, DeviceListBean.class);

    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            mRecyclerView.refresh();
        }
    }

    public class MyAdapterList extends RecyclerView.Adapter<MyAdapterList.ViewHolder> {

        public MyAdapterList() {

        }

        //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.video_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {

            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.img_photo)
            public ImageView img_photo;
            @BindView(R.id.tv_name)
            public TextView mTvName;



            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position){
                DeviceListData listData = mListBean.getList().get(position);
                mTvName.setText(listData.getStnm());
//                BasisApp.loadImg(mContext,Constants.URL_TEST_API+listData.getImgurl(), img_photo, R.drawable.video_img_bg_default);
                BasisApp.loadImg(mContext, MyUtils.getServiceAPI() +listData.getImgurl(), img_photo, R.drawable.video_img_bg_default);
                list_item.setTag(position);
            }


            @OnClick(R.id.list_item)
            public void onItemClick(View v) {
                int position = (int) v.getTag();
                DeviceListData deviceListData = mListBean.getList().get(position);
                Bundle extras = new Bundle();
                extras.putSerializable("bg",deviceListData.getImgurl());
                extras.putSerializable("stcd",deviceListData.getStcd());
                extras.putSerializable("title",deviceListData.getStnm());
                ActivityTool.skipActivity(mContext,VideoInfoActivity.class,extras);
            }
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void searchBtnOnClick(MessageEvent messageEvent) {
        keyword = messageEvent.getMessage();
        getList();
    }

}
